package com.bj58.analysis.tools;

import java.util.HashMap;
import java.util.Map;

import net.sf.json.JSONObject;

import org.apache.commons.lang.StringUtils;

import com.bj58.analysis.tools.common.CookieIdUtils;
import com.bj58.analysis.tools.common.GetObjectToClassUtils;
import com.bj58.analysis.tools.common.InfoDataUtils;
import com.bj58.analysis.tools.common.TrackUrlUtils;
import com.bj58.analysis.tools.common.URLDecodeUtils;
import com.bj58.analysis.tools.common.bean.InfoData;
import com.bj58.analysis.tools.common.bean.LogBean;
import com.bj58.analysis.tools.original.bean.OriginalListClickBean;

/**
 * @description listclick原始日志解析
 * @author tzc
 *
 */
public class OriginalListClickAnalysis extends CommonAnalysis {

	@Override
	protected LogBean analysis(String logLine) {
		OriginalListClickBean bean = null;
		if (StringUtils.isNotBlank(logLine)) {
			String[] logLines = logLine.split("\001");
			if (logLines.length >= 14) {
				InfoData infoData = null;// infoData
				Map<String, String> map = new HashMap<String, String>();

				// cookieId
				String cookieId = logLines[0];
				if (StringUtils.isBlank(cookieId) || cookieId.equals("-")) {
					cookieId = CookieIdUtils.getCookieIdOfRand();
				}
				map.put("cookieID", cookieId);
				map.put("epoch", logLines[1].trim());
				map.put("userIp", logLines[2].trim());

				// 请求参数集
				String requestStr = logLines[5];
				if (StringUtils.isNotBlank(requestStr)
						&& requestStr.indexOf("empty.js.gif?") != -1
						&& requestStr.indexOf("&rand") != -1) {

					String commonParam = requestStr.substring(
							requestStr.indexOf("empty.js.gif?") + 13,
							requestStr.lastIndexOf("&rand"));

					if (StringUtils.isNotBlank(commonParam)) {
						// urlParams
						map.put("urlParams", commonParam);

						Map<String, String> paramMap = URLDecodeUtils
								.parserCommonParams(commonParam);

						if (paramMap != null && paramMap.size() != 0) {
							// trackURL
							String trackUrl = paramMap.get("trackURL");
							// 标准格式化
							trackUrl = TrackUrlUtils.formatTrackUrl(trackUrl);
							if (StringUtils.isNotBlank(trackUrl)) {
								JSONObject jsonObj = null;
								try {
									jsonObj = JSONObject.fromObject(trackUrl);
								} catch (Exception e) {
									e.printStackTrace();
								}
								if (jsonObj != null) {
									// init_refer
									if (jsonObj.containsKey("init_refer")) {
										String init_refer = jsonObj
												.getString("init_refer");
										if (StringUtils.isNotBlank(init_refer)) {
											init_refer = URLDecodeUtils
													.parser(init_refer);
											jsonObj.put("init_refer",
													init_refer);
										}
									}

									// catePath
									if (jsonObj.containsKey("cate")) {
										String catePath = jsonObj
												.getString("cate");
										if (StringUtils.isNotBlank(catePath))
											map.put("catePath", catePath);
									}

									// areaPath
									if (jsonObj.containsKey("area")) {
										String areaPath = jsonObj
												.getString("area");
										areaPath = URLDecodeUtils
												.parser(areaPath);
										if (StringUtils.isNotBlank(areaPath)) {
											if (areaPath.contains("?"))
												areaPath = "";
											jsonObj.put("area", areaPath);
											map.put("areaPath", areaPath);
										}
									}

									// pageType
									if (jsonObj.containsKey("pagetype")) {
										String pagetype = jsonObj
												.getString("pagetype");
										if (StringUtils.isNotBlank(pagetype))
											map.put("pageType", pagetype);
									}

									// page
									if (jsonObj.containsKey("page")) {
										String page = jsonObj.getString("page");
										if (StringUtils.isNotBlank(page))
											map.put("page", page);
									}

									// trackUrl,兼容旧版工具类
									String tmpTrackUrl = jsonObj.toString();
									tmpTrackUrl = tmpTrackUrl.replaceAll("'",
											"");
									tmpTrackUrl = tmpTrackUrl.replaceAll("\"",
											"'");
									map.put("trackUrl", tmpTrackUrl);

								}
							}

							// referrer
							String referrer = paramMap.get("referrer");
							referrer = URLDecodeUtils.parser(referrer);
							if (StringUtils.isNotBlank(referrer))
								map.put("referrer", referrer);

							// infoData
							String infoDataLine = paramMap.get("info_data");
							infoData = InfoDataUtils
									.parserToInfoData(infoDataLine);

						}
					}
				}

				// status
				String status = logLines[6];
				if (StringUtils.isNotBlank(status))
					map.put("status", status);

				// url
				String url = logLines[8];
				url = URLDecodeUtils.parser(url);
				if (StringUtils.isNotBlank(url))
					map.put("url", url);

				// userAgent
				String userAgent = logLines[9];
				if (StringUtils.isNotBlank(userAgent))
					map.put("userAgent", userAgent);

				// responseTime
				String responseTime = logLines[11];
				if (StringUtils.isNotBlank(responseTime))
					map.put("responseTime", responseTime);

				// loginUid
				String loginUid = logLines[13];
				if (StringUtils.isNotBlank(loginUid) && !loginUid.equals("-"))
					map.put("loginUid", loginUid);

				// version
				map.put("version", "v1.0");

				if (map != null && map.size() != 0) {
					Object obj = GetObjectToClassUtils.getObjectByMap(
							OriginalListClickBean.class, map);
					if (obj != null) {
						bean = (OriginalListClickBean) obj;
						if (infoData != null)// infoData
							bean.setInfoData(infoData);
					}
				}
			}

		}
		return bean;
	}

}
